package com.lv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lv.mapper.StudentMapper;
import com.lv.pojo.LoginFrom;
import com.lv.pojo.Student;
import com.lv.service.StudentService;
import com.lv.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/4/28 18:58
 */
@Service("studentServiceImpl")
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Override
    public Student login(LoginFrom loginFrom) {
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",loginFrom.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginFrom.getPassword()));
        Student student = baseMapper.selectOne(queryWrapper);
        return student;
    }

    @Override
    public Student getStudentById(long userId) {
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        Student student = baseMapper.selectOne(queryWrapper);
        return student;
    }

    /**
     * 查询带分页
     * @param page
     * @param student
     * @return
     */
    @Override
    public IPage<Student> getStudentByOpr(Page<Student> page, Student student) {
        QueryWrapper<Student> queryWrapper=null;
        if(null !=student){
            queryWrapper=new QueryWrapper<>();
            if(!StringUtils.isEmpty(student.getClazzName())){
                queryWrapper.eq("clazz_name",student.getClazzName());
            }
            if(!StringUtils.isEmpty(student.getName()))
            {
                queryWrapper.like("name",student.getName());
            }
            queryWrapper.orderByAsc("id");
            queryWrapper.orderByDesc("name");
        }
        Page<Student> studentPage = baseMapper.selectPage(page, queryWrapper);

        return studentPage;
    }

    /**
     * 修改密码
     * @param userId
     * @param oldPwd
     * @return
     */
    @Override
    public Student updatePwd(Long userId, String oldPwd) {
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId.intValue());
        queryWrapper.eq("password",oldPwd);
        Student student = getOne(queryWrapper);
        return student;
    }


}
